/**
 * common
 * @author  fubangfu2015@163.com
 * @data    2020/11/25
 * --------------------------------------
 */
import { emit, on, remove, removeAll } from './event';
import util from './util';

/**
 * 导航
 * @param url
 * @param opt
 */
export function navigate(url, opt = {}) {
  opt.url = url;
  wx.navigateTo(opt);
}

/**
 * 提示
 * @param title
 * @param option
 */
export function tip(title, option = { icon : 'none' }) {
  option.title = title;
  wx.showToast(option);
}

/**
 * 是空值
 * @param val
 * @return {boolean}
 */
export function isEmpty(val) {
  return val === null
    || typeof val === 'undefined'
    || Array.isArray(val) && !val.length;
}

/**
 * 获取bdmin_user_id
 */
export function getBDUserId() {
  return util.getBdmin_user_id();
}

/**
 * 获取share_bdmin_user_id
 */
export function getShareBDUserId() {
  return getBDUserId() || 3;
}

/**
 * 添加全局事件
 * @param name
 * @param self
 * @param cb
 */
export function addListener(name, self, cb) {
  on(name, self, cb);
}

/**
 * 移除全局事件
 * @param self
 * @param name
 */
export function removeListener(self, name) {
  if (arguments.length === 2) {
    remove(name, self);
  } else {
    removeAll(self);
  }
}

/**
 * 触发全局事件
 * @param name
 * @param data
 */
export function emitListener(name, data) {
  emit(name, data);
}
